/*******************************************************************************
 * Copyright (c) 2009-2011 ETCORE Labs. All rights reserved. 
 * 
 * This software is the confidential and proprietary information of ETCORE Labs.
 * You shall not disclose such confidential information and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with ETCORE Labs. 
 * 
 * Support can be obtained from support forums at:  http://www.etcore.com/
 * /
 *******************************************************************************/
package etcore.webapp.grouptalk.common.service;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import etcore.webapp.grouptalk.common.dao.BaseDAO;
import etcore.webapp.grouptalk.common.dao.DirectoryDAO;
import etcore.webapp.grouptalk.common.dao.HibernateSessionFactory;
import etcore.webapp.grouptalk.common.object.Directory;

/**
 * DirectoryService implements queries and transactions on Directory
 * 
 * @author ETCORE GroupTalk Developers
 * 
 */
public class DirectoryService extends BaseService {

	@Override
	public void deleteSingle(Object persistentInstance) {

		Session session = HibernateSessionFactory.getSession();
		Transaction tx = session.beginTransaction();
		try {
			BaseDAO dao = new DirectoryDAO();
			dao.deleteSingle(persistentInstance);
			tx.commit();

		} catch (RuntimeException re) {

			tx.rollback();
		} finally {
			if (session != null)
				session.close();
		}
	}

	/**
	 * Find instances of Directory
	 * 
	 * @param begin
	 *            start position
	 * @param length
	 *            count of instances
	 * @return Directory instances
	 */
	public List<Directory> findAll(int begin, int length) {
		Session session = HibernateSessionFactory.getSession();
		try {
			DirectoryDAO dao = new DirectoryDAO();
			List<Directory> result = dao.findAll(begin, length);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	/**
	 * Find the count of Directory instances
	 * 
	 * @return the count of Directory instances
	 */
	public long findAllSize() {
		Session session = HibernateSessionFactory.getSession();
		try {
			DirectoryDAO dao = new DirectoryDAO();
			long result = dao.findAllSize();

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	/**
	 * Find Directory instances which contain the group
	 * 
	 * @param groupID
	 *            group id
	 * @return Directory instances which contain the group
	 */
	public List<Directory> findByGid(Integer groupID) {

		Session session = HibernateSessionFactory.getSession();
		try {
			DirectoryDAO dao = new DirectoryDAO();
			List<Directory> result = dao.findByGid(groupID);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	/**
	 * Find Directory instance by id
	 * 
	 * @param id
	 *            directory id
	 * @return Directory instance
	 */
	public Directory findById(java.lang.Integer id) {
		Session session = HibernateSessionFactory.getSession();
		try {
			DirectoryDAO dao = new DirectoryDAO();
			Directory result = dao.findById(id);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	/**
	 * Find the first directory instance of the input name
	 * 
	 * @param name
	 *            directory name
	 * @return Directory instance
	 */
	public Directory findByName(String name) {
		Session session = HibernateSessionFactory.getSession();
		try {
			DirectoryDAO dao = new DirectoryDAO();
			Directory result = dao.findByName(name);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	/**
	 * Find Directory instances which belong to the parent platform
	 * 
	 * @param parent
	 *            the parent platform code
	 * @return Directory instances which belong to the parent platform
	 */
	public List<Directory> findByParent(Integer parent) {
		Session session = HibernateSessionFactory.getSession();
		try {
			DirectoryDAO dao = new DirectoryDAO();
			List<Directory> result = dao.findByParent(parent);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}
	public List<Directory> findLocalDirsByParent(Integer parent) {
		Session session = HibernateSessionFactory.getSession();
		try {
			DirectoryDAO dao = new DirectoryDAO();
			List<Directory> result = dao.findLocalDirsByParent(parent);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}
	public List<Directory> findEnglishDirsByParent(Integer parent) {
		Session session = HibernateSessionFactory.getSession();
		try {
			DirectoryDAO dao = new DirectoryDAO();
			List<Directory> result = dao.findEnglishDirsByParent(parent);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}
	/**
	 * Find Directory instances which belong to the parent platform
	 * 
	 * @param parent
	 *            the parent platform code
	 * @param begin
	 *            start position
	 * @param length
	 *            the count of instances
	 * @return Directory instances
	 */
	public List<Directory> findByParent(Integer parent, int begin, int length) {

		Session session = HibernateSessionFactory.getSession();
		try {
			DirectoryDAO dao = new DirectoryDAO();
			List<Directory> result = dao.findByParent(parent, begin, length);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	/**
	 * Find Directory instances by a property value
	 * 
	 * @param propertyName
	 *            property name
	 * @param value
	 *            property value
	 * @return Directory instances
	 */
	public List<Directory> findByProperty(String propertyName, Object value) {
		Session session = HibernateSessionFactory.getSession();
		try {
			DirectoryDAO dao = new DirectoryDAO();
			List<Directory> result = dao.findByProperty(propertyName, value);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	/**
	 * Find Directory instances by a property value
	 * 
	 * @param propertyName
	 *            property name
	 * @param value
	 *            property value
	 * @param begin
	 *            start position
	 * @param length
	 *            the count of instances
	 * @return Directory instances
	 */
	public List<Directory> findByProperty(String propertyName, Object value,
			int begin, int length) {
		Session session = HibernateSessionFactory.getSession();
		try {
			DirectoryDAO dao = new DirectoryDAO();
			List<Directory> result = dao.findByProperty(propertyName, value,
					begin, length);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	/**
	 * Find English Directory instances which belong to the parent platform
	 * 
	 * @param parent
	 *            parent platform code
	 * @return Directory instances
	 */
	public List<Directory> findEnglishDirectoriesByParent(Integer parent) {

		Session session = HibernateSessionFactory.getSession();
		try {
			DirectoryDAO dao = new DirectoryDAO();
			List<Directory> result = dao.findEnglishDirectoriesByParent(parent);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	/**
	 * Find Directory instances of local language which belong to the parent
	 * platform
	 * 
	 * @param parent
	 *            parent platform code
	 * @return Directory instances
	 */
	public List<Directory> findLocalDirectoriesByParent(Integer parent) {

		Session session = HibernateSessionFactory.getSession();
		try {
			DirectoryDAO dao = new DirectoryDAO();
			List<Directory> result = dao.findLocalDirectoriesByParent(parent);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	/**
	 * Find the count of Directory instances which belong to the parent platform
	 * 
	 * @param parent
	 *            parent platform code
	 * @return the count of Directory
	 */
	public long findSizeByParent(Integer parent) {
		Session session = HibernateSessionFactory.getSession();
		try {
			DirectoryDAO dao = new DirectoryDAO();
			long result = dao.findSizeByParent(parent);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	/**
	 * Find Directory instances by a property value
	 * 
	 * @param propertyName
	 *            property name
	 * @param value
	 *            property value
	 * @return the count of Directory instances
	 */
	public long findSizeByProperty(String propertyName, Object value) {

		Session session = HibernateSessionFactory.getSession();
		try {
			DirectoryDAO dao = new DirectoryDAO();
			long result = dao.findSizeByProperty(propertyName, value);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	/**
	 * Get parent platform code
	 * 
	 * @param id
	 *            Directory id
	 * @return parent platform code
	 */
	public long getParent(Integer id) {
		Session session = HibernateSessionFactory.getSession();
		try {
			DirectoryDAO dao = new DirectoryDAO();
			long result = dao.getParent(id);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

}